<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="modalTitle"
    width="800px"
    :show-ok-btn="false"
  >
    <BasicTable
      @register="registerDeptArticlesTable"
      :can-resize="true"
      :bordered="true"
      :striped="true"
      :loading="false"
      :data-source="appointQueryData.articles"
    />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, reactive, unref, inject } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';

  import { queryByDept } from '/@/api/cms/statistics';
  import { articleTableColumns } from './statistics.data';

  import { useMessage } from '/@/hooks/web/useMessage';
  import { OperationTypeEnum } from '/@/enums/appEnum';
  import { BasicTable, useTable } from '/@/components/Table';

  const { createMessage } = useMessage();

  export default defineComponent({
    name: 'DeptArticlesTable',
    components: {
      BasicModal,
      BasicTable,
    },
    emits: ['success', 'register'],
    setup() {
      //弹出窗口界面是关联部门设置
      const op = ref(OperationTypeEnum.UN_KNOWN);
      //标题--根据传入的模块名称+操作类型组合得到
      const modalTitle = ref('标题');
      const moduleName = inject<string>('moduleName');

      //指定部门统计文章数据
      const appointQueryData = reactive({
        articles: [],
      });

      const [registerDeptArticlesTable] = useTable({
        title: '文章列表',
        columns: articleTableColumns, //表头数据
        bordered: true,
        striped: false, //斑马纹
        showIndexColumn: true,
      });

      let queryConditionDto = {
        begintm: '',
        endtm: '',
        ids: [],
        author: '',
        dept: '',
      };

      /**
       * 接收父组件传递的数据并处理
       */
      const [registerModal, { setModalProps }] = useModalInner(async (data) => {
        //接收父组件传递的数据
        console.log(data);
        //判断是否传入op属性
        if (data?.op) {
          op.value = data.op;
        }

        queryConditionDto = {
          begintm: '',
          endtm: '',
          ids: [],
          author: '',
          dept: '',
        };

        //接收查询条件
        if (data?.condition) {
          queryConditionDto.begintm = data.condition.begintm;
          queryConditionDto.endtm = data.condition.endtm;
          queryConditionDto.ids = data.condition.ids;
          queryConditionDto.author = data.condition.author;
          queryConditionDto.dept = data.condition.dept;

          //指定部门数据查询前做清空处理
          if (appointQueryData.articles.length > 0) {
            appointQueryData.articles = [];
          }
          getQueryByDept();
        }

        if (moduleName) {
          modalTitle.value = unref(moduleName);
        }
        if (op.value != OperationTypeEnum.UN_KNOWN && op.value != OperationTypeEnum.CUSTOME) {
          modalTitle.value += '--' + op.value;
        }

        setModalProps({ confirmLoading: false });
      });

      /**
       * 指定部门查询
       */
      async function getQueryByDept() {
        queryByDept(queryConditionDto)
          .then((res) => {
            console.log('指定部门', res);
            //处理返回数据
            res.forEach((val) => {
              appointQueryData.articles.push(val);
            });
          })
          .catch((ex) => {
            console.log('catch ', ex);
            createMessage.error(ex.msg);
          });
      }

      return {
        op,
        moduleName,
        modalTitle,
        registerModal,

        registerDeptArticlesTable,
        getQueryByDept,

        appointQueryData,
      };
    },
  });
</script>
